package com.mcxx.modules.miniSalvation.family.service.write;

import cn.hutool.core.collection.CollectionUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilyMainHistoryWriteDao;
import com.mcxx.modules.miniSalvation.family.dao.write.FamilyMemberHistoryWriteDAO;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMainHistoryEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberHistoryEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service("familyMemberHistoryWriteService")
@TaTransactional
public class FamilyMemberHistoryWriteService {
    @Autowired
    FamilyMainHistoryWriteDao familyMainHistoryWriteDao;
    @Autowired
    FamilyMemberHistoryWriteDAO familyMemberHistoryWriteDao;



    public int delete(FamilyMemberHistoryEntity familyMemberHistory) {
        return familyMemberHistoryWriteDao.delete(familyMemberHistory);
    }

    public int rescoverMemberHistoryEntity(FamilyMainHistoryEntity familyMainHistoryEntity,
                                           List<FamilyMemberHistoryEntity> familyMemberHistoryEntities,
                                           String orderId) {
        familyMainHistoryEntity.setPeopleState(Constant.PeopleState.NORMAL);
        familyMainHistoryEntity.setSuspendFlag(Constant.SuspendFlag.NORMAL_STATE);
        familyMainHistoryEntity.setId(Constant.getUUID());
        familyMainHistoryEntity.setOrderId(orderId);
        familyMainHistoryEntity.setCreateTime(new Date());
        familyMainHistoryWriteDao.insertIgnoreNull(familyMainHistoryEntity);
        for (FamilyMemberHistoryEntity familyMemberHistoryEntity : familyMemberHistoryEntities) {
//            if(Constant.PeopleState.SUSPEND_STATE.equals(familyMemberHistoryEntity.getState())){
//                familyMemberHistoryEntity.setState(Constant.PeopleState.NORMAL);

            familyMainHistoryEntity.setSuspendFlag(Constant.SuspendFlag.NORMAL_STATE);
            familyMemberHistoryEntity.setId(Constant.getUUID());
            familyMemberHistoryEntity.setOrderId(orderId);
            familyMemberHistoryEntity.setCreateTime(new Date());
        }
        int res = 0;
        if (CollectionUtil.isNotEmpty(familyMemberHistoryEntities)) {
            res = familyMemberHistoryWriteDao.insertMemberHistoryEntities(familyMemberHistoryEntities);
        }
        return res;
    }

    public Boolean deleteByOrderId(String orderId) {
        familyMainHistoryWriteDao.deleteByOrderId(orderId);
        return familyMemberHistoryWriteDao.deleteByOrderId(orderId);
    }


    public void copyHistoryRecordByOrderId(String orderId, String newOrderId) {
        int rows = familyMemberHistoryWriteDao.copyHistoryRecordByOrderId(orderId,newOrderId);
        if (rows < 0 ) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void copyHistoryByOrderId(String orderId, String newOrderId) {
        int rows = familyMemberHistoryWriteDao.copyHistoryByOrderId(orderId,newOrderId);
        if (rows < 0 ) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }


    public void batchDeleteByOrderId(List<String> orderIdList) {
        int rows = familyMemberHistoryWriteDao.batchDeleteByOrderId(orderIdList);
        if (rows < 1) {
            throw new BizException(FailureCode.ERR_9999);
        }
    }

    public int updateMemberName(String name,String orderId,String memberId) {
        return familyMemberHistoryWriteDao.updateMemberName(name,orderId,memberId);
    }
}
